<template>
	<view :style="colorStyle">
		<view class="promoter-order">
			<view class='header acea-row row-center-wrapper'>
				<view class='acea-row row-between-wrapper input'>
					<text class='iconfont icon-ic_search'></text>
					<input type='text' placeholder='搜索商品名称' @confirm="searchSubmitValue" confirm-type='search'
						name="search" placeholder-class='placeholder'></input>
				</view>
			</view>
			<timeSlot @changeTime="changeTime"></timeSlot>
			<view class='list' v-if="recordList.length>0">
				<view class="top_num">
					共 <text class="main_color">{{total}}</text> 笔订单，可获得佣金<text class="main_color">¥{{sum_brokerage}}</text>
				</view>
				<block>
					<view class='item'>
						<view class='listn'>
							<block v-for="(child,indexn) in recordList" :key="indexn">
								<view class='itenm acea-flex row-middle'>
									<view  v-if="OrderType==1" style="padding-left: 20rpx;" @click="checkboxChange(child.id)">
										<checkbox :checked="child.checked" />
									</view>
									<view class="" style="width: 100%;">
										<view class='top acea-row row-between-wrapper'>
											<view class='pictxt acea-row row-between-wrapper'>
												<view class='pictrue'>
													<image :src='child.avatar'></image>
												</view>
												<view class='text line1'>{{child.nickname}}</view>
											</view>
											<view class="acea-row row-middle">
												<view class="freeze acea-row row-center-wrapper" v-if="child.is_frozen">冻结</view>
												<view class='money' v-if="child.type == 'brokerage'">返佣：<text
														class='font-color'>¥{{child.number}}</text></view>
												<view class='money' v-else>暂未返佣：<text
														class='font-color'>￥{{child.number}}</text></view>
											</view>
										</view>
										<view class='bottom'>
											<view class="msg">
												<text class='name'>
													<image :src="child.cover" mode="" style="width: 100rpx;height: 100rpx;border-radius: 6rpx;"></image>
												</text>
												<view class="">
													<view class="store_name line1">{{child.store_name}}</view>
													<view class='store_name line1' style="color: #999;font-size: 24rpx;">{{child.guige}}</view>
													<view class='store_name line1' style="color: #999;font-size: 24rpx;"><text style="color: var(--view-theme);font-weight: bold;margin-right: 10rpx;">￥{{child.sum_price}}</text> x{{child.cart_num}}</view>
												</view>
												
											</view>
											<view>实付金额：<text style="color: var(--view-theme);font-weight: bold;">￥{{child.pay_price}}</text></view>
											<view v-if="child.type == 'brokerage'"><text class='name'>返佣时间：{{child.time}}</text></view>
											<view v-else><text class='name'>下单时间：{{child.time}}</text></view>
											<view ><text class='name' @click="makePhone(child.phone)">联系方式：{{child.phone||"15200000000"}}</text></view>
										</view>
									</view>
									
								</view>
							</block>
						</view>
					</view>
				</block>
			</view>
			<view class="empty" v-if="recordList.length == 0">
				<emptyPage title="暂无推广订单～"></emptyPage>
			</view>
			<view class="" style="height: 120rpx;" v-if="OrderType==1"></view>
			<view class="close-account" v-if="recordList.length!=0&&OrderType==1">
				<view class="check-total">
					<view class="check" @click.stop="checkboxAllChange">
						<checkbox :checked="isAllSelect" value="" color="#f20"  />
						<text class="all">全选</text>
					</view>
					<view class="total">
						<text>合计：</text>
						<text class="price">{{selectValue.length}}</text>
					</view>
				</view>
				<view class="account">
					<view class="btn-calculate" @click="seubmite">
						<text>确认签收</text>
					</view>
				</view>
			</view>
		</view>
		<tuiModal :show="showModal" title="提示" content="是否确定核销?" :maskClosable="false" @click="handleClick"></tuiModal>
	</view>
</template>

<script>
	import {
		spreadOrder,
		writeoffs
	} from '@/api/user.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	import timeSlot from '@/components/timeSlot/index.vue'
	import emptyPage from '@/components/emptyPage.vue'
	import home from '@/components/home';
	import colors from '@/mixins/color.js';
	import tuiModal from '@/components/tui-modal/index.vue';
	let sysHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
	export default {
		components: {
			emptyPage,
			home,
			timeSlot,
			tuiModal
		},
		mixins: [colors],
		data() {
			return {
				sysHeight: sysHeight,
				page: 1,
				limit: 10,
				status: false,
				loading: false,
				recordList: [
					
				],
				times: [],
				time: '',
				recordCount: 0,
				count: 0,
				total:0,
				sum_brokerage:0,
				keyword: '',
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				start: 0,
				stop: 0,
				OrderType: 0,
				isAllSelect: false,
				selectValue: [],
				selectCountPrice: 0,
				check_id: [],
				order_no:[],
				showModal:false,
				uid:''
			};
		},
		computed: mapGetters(['isLogin']),
		onLoad(e) {
			this.OrderType = e.type
			this.uid = e.uid
			if (this.isLogin) {
				this.getRecordOrderList();
			} else {
				toLogin()
			}
		},
		onShow() {
			uni.removeStorageSync('form_type_cart');
		},
		methods: {
			searchSubmitValue(e) {
				this.keyword = e ? e.detail.value : ''
				this.page = 1;
				this.limit = 20;
				this.status = false;
				this.$set(this, 'recordList', []);
				this.$set(this, 'times', []);
				this.getRecordOrderList()
			},
			handleClick(e){
				let that = this
				let index = e.index;
				let data = ''
				if (index == 1) {
					that.showModal= false
					that.selectValue.map(item=>{
						data =  item +','+ data
					})
					writeoffs({
						oids:that.selectValue
					}).then(res => {
						that.$util.Tips({
							title:res.msg
						});
						setTimeout(function(){
							that.isAllSelect = false
							that.selectValue = ''
							that.page = 1;
							that.limit = 20;
							that.status = false;
							that.recordList = []
							that.getRecordOrderList()
						},1000)
						
					}).catch(err=>{
						console.log(err)
						that.$util.Tips({
							title:err
						});
					});
				}
			},
			makePhone: function(phone) {
				let that = this;
				// #ifdef APP-PLUS
				plus.device.dial(phone, true);
				// #endif
				// #ifdef MP || H5
				uni.makePhoneCall({
				    phoneNumber: phone
				});
				// #endif
			},
			changeTime(time) {
				this.start = time.start
				this.stop = time.stop
				this.selectValue = ''
				this.isAllSelect = false
				this.searchSubmitValue()
			},
			onLoadFun() {
				this.getRecordOrderList();
				this.isShowAuth = false
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			seubmite(){
				let that = this;
				that.showModal = true
			},
			getRecordOrderList() {
				let that = this;
				let page = that.page;
				let limit = that.limit;
				let status = that.status;
				if (that.loading) return
				if (status == true) return;
				that.loading = true;
				spreadOrder({
					start: this.start,
					stop: this.stop,
					keyword: this.keyword,
					page: page,
					limit: limit,
					userId:this.uid
				}).then(res => {
					this.total = res.data.count;
					this.sum_brokerage = res.data.sum_brokerage;
					res.data.list.map(item=>{
						item.checked = false
						item.cover =item.store_info[item.cart_id[0]].cart_info.productInfo.image
						item.cart_num =item.store_info[item.cart_id[0]].cart_info.cart_num
						item.guige =item.store_info[item.cart_id[0]].cart_info.productInfo.attrInfo.suk
						item.sum_price = item.store_info[item.cart_id[0]].cart_info.sum_price
					})
					this.recordList = this.recordList.concat(res.data.list)
					console.log(this.recordList)
					that.count = res.data.count || 0;
					that.status = res.data.list.length < 5;
					that.page += 1;
					that.loading = false;
					return
					for (let i = 0; i < res.data.time.length; i++) {
						if (!this.times.includes(res.data.time[i].time)) {
							this.times.push(res.data.time[i].time)
							this.recordList.push({
								time: res.data.time[i].time,
								count: res.data.time[i].count,
								child: [],
							})
						}
					}
					for (let x = 0; x < this.times.length; x++) {
						for (let j = 0; j < res.data.list.length; j++) {
							if (this.times[x] === res.data.list[j].time_key) {
								this.recordList[x].child.push(res.data.list[j])
							}
						}
					}
					console.log(this.recordList)
					that.count = res.data.count || 0;
					that.status = res.data.list.length < 5;
					that.page += 1;
					that.loading = false;
				}).catch(err=>{
					that.loading = false;
				});
			},
			checkboxChange(e) {
				console.log(e)
				let that = this;
				let value = [];
				let valid = that.recordList;
				let arr1 = [];
				let arr2 = [];
				let arr3 = [];
				if (that.check_id.length == 0) {
					that.check_id.push(e)
				} else {
					if (that.check_id.indexOf(e) == -1) {
						that.check_id.push(e)
					} else {
						console.log(e)
						that.check_id.map((item, index) => {
							if (item == e) {
								that.check_id.splice(index, 1)
							}
						})
					}
				}
				
				let newValid = valid.map(item => {
					if (that.inArray(item.id, that.check_id)) {
						item.checked = true;
						arr1.push(item);
					} else {
						item.checked = false;
						arr2.push(item);
					}
					return item;
				});
			
				that.$set(that.recordList, 'valid', newValid);
			
				that.isAllSelect = newValid.length === arr1.length
				that.selectValue = that.check_id;
			},
			checkboxAllChange() {
				this.isAllSelect = !this.isAllSelect
				if (this.isAllSelect) {
					this.setAllSelectValue(1)
				} else {
					this.setAllSelectValue(0)
				}
			},
			setAllSelectValue: function(status) {
				let that = this;
				let selectValue = [];
				let order_no = []
				let valid = that.recordList
				if (valid.length > 0) {
					let newValid = valid.map(item => {
						if (status) {
							item.checked = true;
							selectValue.push(item.id);
							order_no.push(item.order_no)
							that.isAllSelect = true;
						} else {
							item.checked = false;
							that.isAllSelect = false;
						}
						return item;
					});
					that.$set(that.recordList, 'valid', newValid);
					that.selectValue = selectValue;
					that.order_no = order_no
				}
			},
			inArray: function(search, array) {
				for (let i in array) {
					if (array[i] == search) {
						return true;
					}
				}
				return false;
			},
		},
		onReachBottom: function() {
			this.getRecordOrderList();
		}
	}
</script>

<style scoped lang="scss">
	.empty{
		margin: 20rpx;
	}
	.sys-title {
		z-index: 10;
		position: relative;
		height: 40px;
		line-height: 40px;
		font-size: 30rpx;
		color: #fff;
		background-color: var(--view-theme);
		// #ifdef MP || APP-PLUS
		text-align: center;
		// #endif
	}

	.sys-head {
		background-color: #fff;
	}

	.promoter-order .header {
		width: 100%;
		height: 96rpx;
		background-color: #fff;
		padding-top: 24rpx;
	}

	.promoter-order .header .input {
		width: 710rpx;
		height: 72rpx;
		background-color: #f5f5f5;
		border-radius: 50rpx;
		box-sizing: border-box;
		padding: 0 32rpx;
	}

	.promoter-order .header .input .iconfont {
		font-size: 32rpx;
		color: #999;
	}

	.promoter-order .header .input .placeholder {
		color: #ccc;
	}

	.promoter-order .header .input input {
		font-size: 26rpx;
		height: 100%;
		width: 597rpx;
	}

	.promoter-order .list .item .title {
		height: 133rpx;
		padding: 0 30rpx;
		font-size: 26rpx;
		color: #999;
	}

	.promoter-order .list .item .title .data {
		font-size: 28rpx;
		color: #282828;
		margin-bottom: 5rpx;
	}

	.promoter-order .list .item .listn .itenm {
		background-color: #fff;
		margin: 0 20rpx 20rpx 20rpx;
		border-radius: 14rpx;
	}

	.promoter-order .list .item .listn .itenm~.itenm {
		margin-top: 12rpx;
	}

	.promoter-order .list .item .listn .itenm .top {
		margin-left: 20rpx;
		padding-right: 20rpx;
		border-bottom: 1rpx solid #eee;
		height: 128rpx;
		
		.freeze{
			width: 72rpx;
			height: 40rpx;
			background: #FFF1E5;
			border-radius: 8rpx;
			font-size: 24rpx;
			color: #FF7D00;
			margin-right: 16rpx;
		}
	}

	.promoter-order .list .item .listn .itenm .top .pictxt {
		width: 320rpx;
	}

	.promoter-order .list .item .listn .itenm .top .pictxt .text {
		width: 240rpx;
		font-size: 30rpx;
		color: #333;
	}

	.promoter-order .list .item .listn .itenm .top .pictxt .pictrue {
		width: 64rpx;
		height: 64rpx;
	}

	.promoter-order .list .item .listn .itenm .top .pictxt .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.promoter-order .list .item .listn .itenm .top .money {
		font-size: 28rpx;
		
		.font-color{
			font-size: 32rpx;
			font-family: "Regular";
		}
	}

	.promoter-order .list .item .listn .itenm .bottom {
		padding: 28rpx 20rpx;
		font-size: 28rpx;
		color: #666;
		line-height: 1.6;
		.msg{
			display: flex;
			margin-bottom: 6rpx;
		}
	}

	.promoter-order .list .item .listn .itenm .bottom .name {
		color: #999999;
	}

	.promoter-order .list .item .listn .itenm .bottom .store_name {
		width: 480rpx;
		color: #666666;padding-left: 10rpx;
	}
	.top_num{
		padding: 28rpx 20rpx;
		font-size: 24rpx;
		color: #999;
	}
	.main_color{
		color: #E93323;
		margin: 0 6rpx;
	}
	.close-account {
		position: fixed;
		left: 0;
		bottom: 0rpx;
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 100rpx;
		background-color: #FFFFFF;
		border-top: 2rpx solid #f6f6f6;
	
		.check-total {
			display: flex;
			align-items: center;
			width: 50%;
			height: 100%;
	
			.check {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 40%;
				height: 100%;
	
				text {
					font-size: 36rpx;
					color: #333333;
				}
	
				.icon-checked {
					color:var(--view-theme);
					// box-shadow: 0 0 10rpx $base;
				}
	
				.all {
					font-size: 24rpx;
					margin-left: 10rpx;
				}
			}
	
			.total {
				display: flex;
				align-items: center;
				width: 60%;
				height: 100%;
	
				text {
					font-size: 24rpx;
					color: #333333;
				}
	
				.price {
					font-weight: bold;
					color: var(--view-theme);
				}
			}
		}
	
		.account {
			display: flex;
			align-items: center;
			justify-content: flex-end;
			width: 46%;
			padding-right: 4%;
	
			.btn-calculate {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 160rpx;
				height: 60rpx;
				background-color: var(--view-theme);
				border-radius: 60rpx;
	
				text {
					color: #FFFFFF;
					font-size: 24rpx;
				}
			}
	
			.btn-del {
				display: flex;
				align-items: center;
				justify-content: space-between;
	
				.attention {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 140rpx;
					height: 60rpx;
					border: 2rpx solid #EEEEEE;
					border-radius: 60rpx;
					color: #333333;
					font-size: 24rpx;
					margin-right: 20rpx;
				}
	
				.del {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 100rpx;
					height: 60rpx;
					background-color: var(--view-theme);
					border-radius: 60rpx;
					color: #FFFFFF;
					font-size: 24rpx;
				}
			}
		}
	}
</style>
